import { FastPageApi } from "@/api";
import SubjectItems from "@/components/SubjectItems";
import React, { useEffect, useState } from "react";
import "./Fast.less";
interface IFastData {
  subjects: IRecords[];
}
//定义自定义hook抽离首页数据
function useFastData(): [IFastData, () => void] {
  // 当前设置的大学科下面的每个小的题目分类
  const [subjects, setSubjects] = useState<IRecords[]>([]); // useState可以传递泛型，用来约定 前面的数据的类型
  function getFastData() {
    FastPageApi().then((res) => {
      console.log(res);
      if (res.errCode === 0) {
        //更新数据
        setSubjects(res.data.records);
      }
    });
  } //把首页的数据请求回来，因为我们不会再次获取这个数据，可以把请求也一并写入自定义hook
  useEffect(() => {
    getFastData();
  }, []);
  const fastData: IFastData = { subjects };
  return [fastData, getFastData];
}
export default function Fast() {
  let [fastData, getFastData] = useFastData();
  // useEffect(() => {
  //   FastPageApi().then((res) => {
  //     console.log(res.data.records);
  //   });
  // }, []);
  return (
    <section className="jjj">
      {fastData.subjects.map((item) => (
        <SubjectItems data={item} key={item.id} />
      ))}
    </section>
  );
}
